using UnityEngine;
using System.Collections;

/// \class  gkUtilityBounds
/// \brief  Compute bounds
public class gkUtilityBounds 
{
	/// \brief  Grab the Game Object bounds using prioritary the renderer bounds 
	// or the collider bounds if no renderer bounds have been found 
	public static bool GrabRendererOrColliderBounds(GameObject a_rGameObject, out Bounds a_rBounds)
	{
		if(a_rGameObject.renderer != null)
		{
			a_rBounds = a_rGameObject.renderer.bounds;
			return true;
		}
		else if(a_rGameObject.collider != null)
		{
			a_rBounds = a_rGameObject.collider.bounds;
			return true;
		}
		else
		{
			a_rBounds = new Bounds(Vector3.zero, Vector3.zero);
			return false;
		}
	}
	
	/// \brief  Grab the Game Object bounds using prioritary the collider bounds 
	// or the renderer bounds if no renderer bounds have been found 
	public static bool GrabColliderOrRendererBounds(GameObject a_rGameObject, out Bounds a_rBounds)
	{
		if(a_rGameObject.collider != null)
		{
			a_rBounds = a_rGameObject.collider.bounds;
			return true;
		}
		else if(a_rGameObject.renderer != null)
		{
			a_rBounds = a_rGameObject.renderer.bounds;
			return true;
		}
		else
		{
			a_rBounds = new Bounds(Vector3.zero, Vector3.zero);
			return false;
		}
	}
	
	/// \brief  Grab the Game Object bounds using the collider bounds 
	public static bool GrabColliderBounds(GameObject a_rGameObject, out Bounds a_rBounds)
	{
		if(a_rGameObject.collider != null)
		{
			a_rBounds = a_rGameObject.collider.bounds;
			return true;
		}
		else
		{
			a_rBounds = new Bounds(Vector3.zero, Vector3.zero);
			return false;
		}
	}
	
	/// \brief  Grab the Game Object bounds using the renderer bounds 
	public static bool GrabRendererBounds(GameObject a_rGameObject, out Bounds a_rBounds)
	{
		if(a_rGameObject.renderer != null)
		{
			a_rBounds = a_rGameObject.renderer.bounds;
			return true;
		}
		else
		{
			a_rBounds = new Bounds(Vector3.zero, Vector3.zero);
			return false;
		}
	}
	
	/// \brief  Test if a game object contain a screen point
	public static bool ContainsScreenPoint(GameObject a_rGameObject, Vector2 a_f2ScreenPosition)
	{
		Ray oOutOfScreenRay;
		Bounds oGameObjectBounds;
		
		// Compute the ray from the main camera eye in the out of the screen direction
		oOutOfScreenRay = Camera.main.ScreenPointToRay(a_f2ScreenPosition);
	    
		// Get the game object bounds
		GrabColliderOrRendererBounds(a_rGameObject, out oGameObjectBounds);
				
		// Test if the game object is intersected by the screen ray
		return oGameObjectBounds.IntersectRay(oOutOfScreenRay);
	}
}
